cmake_minimum_required(VERSION 3.15)

project(lua54)

set(LUA_SOURCE_PATTERN ${CMAKE_CURRENT_LIST_DIR}/src/*.c)

file(GLOB_RECURSE LUA_SOURCE_FILES ${LUA_SOURCE_PATTERN})

list(FILTER LUA_SOURCE_FILES EXCLUDE REGEX "lua.c")
list(FILTER LUA_SOURCE_FILES EXCLUDE REGEX "luac.c")

if(MSVC)
	list(FILTER LUA_SOURCE_FILES EXCLUDE REGEX "thread_time.c")
endif()

add_library(liblua54 ${LUA_SOURCE_FILES})

add_executable(lua54 ${LUA_SOURCE_FILES} ${CMAKE_CURRENT_LIST_DIR}/src/lua.c)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
	target_link_libraries(lua54 PRIVATE dl m pthread)
endif()

target_compile_definitions(liblua54
PRIVATE LUA_COMPAT_5_3
PRIVATE ENABLE_MULTI_THREAD
)

if(MSVC)
	add_library(lua54_shared SHARED ${LUA_SOURCE_FILES})
	target_compile_definitions(lua54_shared 
	PRIVATE LUA_COMPAT_5_3
	PRIVATE LUA_BUILD_AS_DLL)
	set_property(TARGET lua54_shared PROPERTY OUTPUT_NAME "lua54")
else()
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -O2 -Wall -Wextra")

	set_property(TARGET liblua54 PROPERTY OUTPUT_NAME "lua54")

	target_compile_definitions(liblua54
	PRIVATE LUA_USE_LINUX
	)
	target_compile_definitions(lua54
	PRIVATE LUA_USE_LINUX
	)
endif()
